<?php

namespace App\Console\Commands;

use App\Enums\SmsTempEnum;
use App\Jobs\SendChangeOrderMessage;
use App\Jobs\SendSmsMessageJob;
use App\Models\Order;
use App\Models\OrderLease;
use App\Services\AlipayMessageService;
use App\Services\OrderService;

class AccountIssuanceReminderCommand extends BaseCommand
{
    const LIMIT_NUM = 1;//一个月发送短信次数

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'AccountIssuanceReminder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单账单出账';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    protected function handleExec()
    {
        try{
            $leaseStatus = [
                OrderLease::ORDER_LEASE_STATUS_NOPAY,
                OrderLease::ORDER_LEASE_STATUS_OVERDUE,
            ];
            $where = [
                ['ol.begin_time','<=',strtotime(date('Y-m-d 23:59:59',strtotime('+3 day')))],
                ['ol.begin_time','>=',strtotime(date('Y-m-d ',strtotime('+3 day')))],
            ];
            //数据
            $data = Order::from('sp_order_2 as o')
                ->select('o.id','o.status as order_status','ol.id as lease_id','ol.paid_rent','ol.overdue_amount','ol.begin_time','ol.status as lease_status','ol.rent')
                ->leftjoin('sp_order_lease_2 as ol', 'o.id','ol.order_id')
                ->whereIn('o.status',OrderService::getOrderStatus(OrderService::ORDER_OVERDUE_COUNT))
                ->whereIn('ol.status',$leaseStatus)
                ->where($where)
                ->orderBy('ol.id', 'asc')
                ->get();
            $data = obToArray($data);
            if($data){
                foreach ($data as $item){
                    SendChangeOrderMessage::dispatch($item['id'],AlipayMessageService::CONSUMER,'BILLED');
                    SendSmsMessageJob::dispatch(SmsTempEnum::PAYMENT_DUE_SOON,$item['id'],$item['lease_id']);
                }

                $this->infoLog('出账消息发送执行成功',$data);
            }
        }catch (\Exception $exception){
            $this->infoLog('出账消息发送执行失败'.$exception->getMessage());
        }

        try{
            $leaseStatus = [
                OrderLease::ORDER_LEASE_STATUS_NOPAY,
                OrderLease::ORDER_LEASE_STATUS_OVERDUE,
            ];
            $where = [
                ['ol.begin_time','<=',strtotime(date('Y-m-d 23:59:59',time()))],
                ['ol.begin_time','>=',strtotime(date('Y-m-d ',time()))],
            ];
            //数据
            $data = Order::from('sp_order_2 as o')
                ->select('o.id','o.status as order_status','ol.id as lease_id','ol.paid_rent','ol.overdue_amount','ol.begin_time','ol.status as lease_status','ol.rent')
                ->leftjoin('sp_order_lease_2 as ol', 'o.id','ol.order_id')
                ->whereIn('o.status',OrderService::getOrderStatus(OrderService::ORDER_OVERDUE_COUNT))
                ->whereIn('ol.status',$leaseStatus)
                ->where($where)
                ->orderBy('ol.id', 'asc')
                ->get();
            $data = obToArray($data);
            if($data){
                foreach ($data as $item){
                    SendChangeOrderMessage::dispatch($item['id'],AlipayMessageService::CONSUMER,'PAY_WAITING');
                    SendSmsMessageJob::dispatch(SmsTempEnum::PAYMENT_DUE_TODAY,$item['id'],$item['lease_id']);
                }

                $this->infoLog('当日还款发送执行成功',$data);
            }
        }catch (\Exception $exception){
            $this->infoLog('当日还款发送执行失败'.$exception->getMessage());
        }

        try{

            //数据
            $data = Order::from('sp_order_2 as o')
                ->select('o.id')
                ->whereIn('o.status',['38','39'])
                ->orderBy('o.id', 'asc')
                ->get();
            $data = obToArray($data);
            if($data){
                foreach ($data as $item){
                    SendChangeOrderMessage::dispatch($item['id'],AlipayMessageService::CONSUMER,'OVERDUE');
                    SendSmsMessageJob::dispatch(SmsTempEnum::PAYMENT_FAILED,$item['id'],$item['lease_id'])->delay(1800);
                    SendSmsMessageJob::dispatch(SmsTempEnum::PAYMENT_FAILED,$item['id'],$item['lease_id'])->delay(10800);
                }
                $this->infoLog('逾期消息发送执行成功',$data);
            }
        }catch (\Exception $exception){
            $this->infoLog('逾期消息发送执行失败'.$exception->getMessage());
        }



    }


}
